//
//  WorkExperienceViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/6.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "WorkExperienceViewController.h"
#import "TypeOfWorkViewController.h"
#import "WorkerTypeModel.h"
#import "ITDatePickerController.h"

static NSString *cellPath = @"WorkExperCell";
@interface WorkExperienceViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,ITDatePickerControllerDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *titAry;
@property (nonatomic, strong)WorkerTypeModel *typeModel;
@property (nonatomic, strong)NSString *startStr;
@property (nonatomic, strong)NSString *endStr;
@property (nonatomic, strong)NSString *companyStr;
@property (nonatomic, strong)NSString *numStr;
@property (nonatomic, strong)NSString *comtypeStr;
@property (nonatomic, strong)UILabel *comTypeLab;
@property (nonatomic, assign)BOOL isEnd;
@property (strong, nonatomic) NSDate *startDate;
@property (strong, nonatomic) NSDate *endDate;

@end

@implementation WorkExperienceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _startStr = @"开始时间";
    _endStr = @"结束时间";
    _companyStr = @"";
    _numStr = @"";
    _comtypeStr = @"";
    if (_isEdit)
    {
        _startStr = [_experModel.startDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        _endStr = [_experModel.endDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        _companyStr = _experModel.department;
    }
    _titAry = @[@"工作单位",@"起止时间"];//@"工龄",@"工种"
    [self creatUI];
}

- (void)creatUI
{
    self.navigationItem.title = @"工作经历";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    UIButton *rightBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, 40, 30) ImageName:nil Target:self Action:@selector(saveAction) Title:@"保存" Font:16.0];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = barbtn;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90*displaywidth) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = BacksColor;
    [_tableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [self.view addSubview:_tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _titAry[indexPath.row];
    cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
    cell.textLabel.textColor = RGB(77, 77, 77);
    if (indexPath.row == 0 )
    {
//        NSArray *playderAry = @[@"",@"请输入学校名字",@"请输入工龄(年)",@""];
//        NSArray *textAry = @[@"",_companyStr,_numStr,@""];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(100*displaywidth, 5*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) placeholder:@"请输入工作单位名称" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
        [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = RGB(77, 77, 77);
        textfield.textAlignment = NSTextAlignmentRight;
        textfield.text = _companyStr;
        textfield.delegate = self;
        textfield.tag = 100+indexPath.row;
        [cell.contentView addSubview:textfield];
    }
//    else if (indexPath.row == 3)
//    {
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        UILabel *baseLabel = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 2.5*displayheight, SCREEN_WIDTH-130*displaywidth, 40*displayheight) Font:13.0 Text:_comtypeStr Color:RGB(77, 77, 77)];
//        baseLabel.textAlignment = NSTextAlignmentRight;
//        [cell.contentView addSubview:baseLabel];
//        _comTypeLab = baseLabel;
//
//    }
    else
    {
        UIButton *startBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-200*displaywidth, 2.5*displaywidth, 80*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(startTimrAction:) Title:_startStr Font:12.0];
        [startBtn setTitleColor:RGB(179, 179, 179) forState:UIControlStateNormal];
        startBtn.backgroundColor = [UIColor whiteColor];
//        startBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//        startBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [cell.contentView addSubview:startBtn];
        UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-120*displaywidth, 5*displaywidth, 20*displaywidth, 35*displaywidth) Font:12.0 Text:@"-" Color:RGB(179, 179, 179)];
        tostLab.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:tostLab];
        UIButton *endBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-100*displaywidth, 2.5*displaywidth, 80*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(endTimrAction:) Title:_endStr Font:12.0];
        [endBtn setTitleColor:RGB(179, 179, 179) forState:UIControlStateNormal];
        endBtn.backgroundColor = [UIColor whiteColor];
//        endBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        endBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [cell.contentView addSubview:endBtn];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 3)
//    {
//        TypeOfWorkViewController *vc = [[TypeOfWorkViewController alloc] init];
//        WEAKSELF;
//        vc.closeTypeblock = ^(WorkerTypeModel *model)
//        {
//            weakSelf.typeModel = model;
//            weakSelf.comTypeLab.text = model.name;
//        };
//        [self.navigationController pushViewController:vc animated:YES];
//    }
}

- (void)startTimrAction:(UIButton *)btn
{
    _isEnd = NO;
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 200;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    datePickerController.defaultDate = self.startDate;
    datePickerController.maximumDate = self.endDate;
    
    [self presentViewController:datePickerController animated:YES completion:nil];
}

- (void)endTimrAction:(UIButton *)btn
{
    _isEnd = YES;
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 300;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    datePickerController.defaultDate = self.startDate;
    datePickerController.maximumDate = self.endDate;
    
    [self presentViewController:datePickerController animated:YES completion:nil];
}

- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString {
    
    if (!_isEnd)
    {
        _startStr = dateString;
//        [dateString stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    }
    else
    {
        _endStr = dateString;
//        [dateString stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    }
    if (datePickerController.tag == 200) {
        self.startDate = date;
    } else {
//        self.endDate = date;
    }
    [self.tableview reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45*displaywidth;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    _companyStr = textField.text;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    return indexPath;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

- (void)saveAction
{
    [self.view endEditing:YES];
    if ([_companyStr isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入工作单位名称" addToView:self.view];
        return;
    }
    if ([_startStr isEqualToString:@"开始时间"])
    {
        [LTools showMBProgressWithText:@"请选择开始时间" addToView:self.view];
        return;
    }
    if ([_endStr isEqualToString:@"结束时间"])
    {
        [LTools showMBProgressWithText:@"请选择结束时间" addToView:self.view];
        return;
    }
    WEAKSELF;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic setObject:[_startStr stringByReplacingOccurrencesOfString:@"." withString:@"-"] forKey:@"startDate"];
    [dic setObject:[_endStr stringByReplacingOccurrencesOfString:@"." withString:@"-"] forKey:@"endDate"];
    [dic setObject:_companyStr forKey:@"department"];
    if (_isEdit)
    {
        [dic setObject:_experModel.ID forKey:@"id"];
    }
    [HYBaseHandler requestExperienceSaveParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"保存成功" addToView:sharedDelegate.window];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
